﻿<?php
require_once(dirname(__FILE__).'/QuizModel.php');
require_once(dirname(__FILE__).'/QuizValidate.php');
$act = $_REQUEST['act'];
$aryAnsItem = array('A','B', 'C', 'D');
$numberQuizPerPage = 1;//so cau hoi trong mo de bai 
if ($act == 'cate-add'){
	$name = htmlspecialchars(trim($_REQUEST['name']));
	$active = 1;
	$quizModel = new QuizModel();
	// 1.them cate moi
	$quizModel->addCate(array('name'=>$name, 'active'=>1));
	// 2.hien thi danh sach cate
	$res = $quizModel->getCate();
	require_once(dirname(__FILE__).'/template/quiz-cate-admin.php');
	
} elseif ($act == 'cate-edit') {
	$id = (int)$_REQUEST['id'];
	$name = htmlspecialchars(trim($_REQUEST['name']));
	$active = (int)$_REQUEST['active'];
	$quizModel = new QuizModel();
	// 1.edit cate
	$quizModel->editCate($id,array('name'=>$name, 'active'=>$active));
	// 2.hien thi danh sach cate
	$res = $quizModel->getCate();
	require_once(dirname(__FILE__).'/template/quiz-cate-admin.php');
} elseif ($act == 'cate-delete') {
	$id = (int)$_REQUEST['id'];
	$quizModel = new QuizModel();
	// 1.delete cate
	$quizModel->delCate($id);
	// 2.hien thi danh sach cate
	$res = $quizModel->getCate();
	require_once(dirname(__FILE__).'/template/quiz-cate-admin.php');

} elseif ($act == 'cate-list')  {
	$quizModel = new QuizModel();
	$res = $quizModel->getCate();
	require_once(dirname(__FILE__).'/template/quiz-cate-admin.php');
} elseif ($act == 'quiz-list')  {
	$cateId = (int)$_REQUEST['cate_id'];
	$testId = (int)$_REQUEST['test_id'];
	$quizModel = new QuizModel();
	// 1. danh sach cau hoi
	$res = $quizModel->getQuiz($cateId, $testId * $numberQuizPerPage, $numberQuizPerPage);
	foreach ($res as $key=>$row) {
		$res[$key]['aryChoice'] = explode('|||',$row['choices']);
	}
	// 2. danh sach cate
	$cates = $quizModel->getCate();	
	// 3. tu so luong quiz tao ra danh sach cac de cua cate do
	$cate = $quizModel->getCateById($cateId);
	$numberTest = ceil($cate['number_quiz']/$numberQuizPerPage);
	
	require_once(dirname(__FILE__).'/template/quiz-list-admin.php');
} elseif ($act == 'quiz-edit')  {
	if ($_REQUEST['btnSubmit']) {
		$quiz['id'] = (int)$_REQUEST['id'];
		$quiz['cate_id'] = (int)$_REQUEST['cate_id'];
		$quiz['content'] = trim($_REQUEST['content']); //mce editor da escape html tags roi
		$quiz['choices'] = htmlspecialchars(implode('|||',$_REQUEST['choices']), ENT_QUOTES);
		$quiz['answer'] = (int)$_REQUEST['answer'];
		$quiz['active'] = (int)$_REQUEST['active'];

		$quizModel = new QuizModel();
		$quizValidate = new QuizValidate();
		$validate = $quizValidate->validateQuiz($quiz);
		if ($validate['isOk'] > 0){
			// 1.edit quiz
			$quizModel->editQuiz($quiz['id'],array('content'=>$quiz['content'], 'choices'=>$quiz['choices'], 'answer'=>$quiz['answer'], 'active'=>$quiz['active']));
		}
		// 2.hien thi quiz
		$quiz['aryChoice'] = explode('|||',$quiz['choices']);
		$isOk = $validate['isOk'];
		// 3.hien thi danh sach cate
		$cates = $quizModel->getCate();
		require_once(dirname(__FILE__).'/template/quiz-edit-admin.php');

	} else {
		$quizModel = new QuizModel();
		$id = (int)$_REQUEST['id'];
		$quizModel = new QuizModel();
		// 1.hien thi danh sach cate
		$cates = $quizModel->getCate();
		// 2.hien thi quiz
		$quiz = $quizModel->getQuizById($id);
		$quiz['aryChoice'] = explode('|||',$quiz['choices']);
		require_once(dirname(__FILE__).'/template/quiz-edit-admin.php');
	}
}
?>